package com.itfreer.arcgis;

import android.app.Activity;
import android.graphics.Point;
import android.util.DisplayMetrics;

import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.mapping.view.MapView;

/**
 * 地图视图操作
 */
public class MapViewUtils
{
    /**
     * 地图缩放
     * @param scale
     * @param  mapView
     */
    public static void ZoomView(MapView mapView, double scale)
    {
        if(mapView == null)
            return;
        mapView.setViewpointScaleAsync(scale);
    }

    /**
     * 默认放大
     * @param mapView
     */
    public static  void DefaultZoomIn(MapView mapView)
    {
        if(mapView == null)
            return;
        double scale = mapView.getMapScale();
        ZoomView(mapView,scale*0.5);
    }

    /**
     * 默认缩小
     * @param mapView
     */
    public static  void DefaultZoomOut(MapView mapView)
    {
        if(mapView == null)
            return;
        double scale = mapView.getMapScale();
        ZoomView(mapView,scale*2);
    }

    /**
     * 显示或者隐藏指定索引图层
     * @param mapView
     */
    public static void ShowOrHiddenLayer(MapView mapView,Integer index)
    {
        if(mapView== null || index<0 || index>mapView.getMap().getOperationalLayers().size()-1)
            return;
        Layer layer = MapUtils.GetLayerByIndex(mapView,index);
        if(layer == null)
            return;
        Boolean visible = layer.isVisible();
        if(layer.canChangeVisibility())
            layer.setVisible(!visible);
    }


    /**
     * 修改指定图层顺序
     * @param mapView
     */
    public static void ChangeLayerOrder(MapView mapView,Layer layer,Integer order)
    {
        if(mapView == null || layer == null || order<0 || order>mapView.getMap().getOperationalLayers().size()-1)
            return;

        //删除当前图层
        mapView.getMap().getOperationalLayers().remove(layer);
        mapView.getMap().getOperationalLayers().add(order,layer);
    }

    /**
     * 获取屏幕中心坐标点
     * @param mapView
     * @return
     */
    public static com.esri.arcgisruntime.geometry.Point GetScreenCenterGISPoint(MapView mapView)
    {
        if(mapView == null)
            return  null;

        Point point = GetScreenCenterPoint((Activity) mapView.getContext());
        if(point == null)
            return  null;

        return  mapView.screenToLocation(point);
    }

    /**
     * 获取屏幕坐标
     * @param context
     * @return
     */
    public static Point GetScreenCenterPoint(Activity context)
    {
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        Integer  width = metrics.widthPixels;
        Integer height = metrics.heightPixels;
        return  new Point(width/2,height/2);
    }
}
